package hys.wssoc.modules.info.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hys.wssoc.common.utils.PageUtils;
import hys.wssoc.common.utils.Query;
import hys.wssoc.modules.constant.RedisConstants;
import hys.wssoc.modules.info.dao.CityDao;
import hys.wssoc.modules.info.entity.CityEntity;
import hys.wssoc.modules.info.service.CityService;
import hys.wssoc.modules.info.service.CountrysideService;
import hys.wssoc.modules.info.service.IRedisService;
import hys.wssoc.modules.info.utils.CommonUtils;
import hys.wssoc.modules.info.vo.CitySummaryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("cityService")
public class CityServiceImpl extends ServiceImpl<CityDao, CityEntity> implements CityService {
    @Autowired
    private CityDao cityDao;

    @Autowired
    private CountrysideService countrysideService;

    @Autowired
    private IRedisService redisService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CityEntity> page = this.page(
                new Query<CityEntity>().getPage(params),
                new QueryWrapper<CityEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CitySummaryVo> selectList(String provinceName) {
        List<CityEntity> summaryList;
        if (redisService.exists(RedisConstants.CITY + "summaryList_" + provinceName)){
            String redisValue = redisService.get(RedisConstants.CITY + "summaryList_" + provinceName);
            summaryList = JSON.parseArray(redisValue, CityEntity.class);
        }else {
            QueryWrapper<CityEntity> wrapper = new QueryWrapper<>();
            wrapper.select("city_name", "ratio", "countryside_num", "rs_num");
            wrapper.eq("province_name", provinceName);
            summaryList = cityDao.selectList(wrapper);
            redisService.set(RedisConstants.CITY + "summaryList_" + provinceName, JSON.toJSONString(summaryList), 86400L);
        }
        // 将数据库返回值赋值给VO对象返回给Controller层
        return summaryList.size() == 0 ? new ArrayList<>() : CommonUtils.transformClass4List(summaryList, CitySummaryVo.class);
    }

    @Override
    public CitySummaryVo detail(String cityName) {
        CitySummaryVo cityDetail;
        if (redisService.exists(RedisConstants.CITY + "detail_" + cityName)){
            String redisValue = redisService.get(RedisConstants.CITY + "detail_" + cityName);
            cityDetail = JSON.parseObject(redisValue, CitySummaryVo.class);
        } else {
            QueryWrapper<CityEntity> wrapper = new QueryWrapper<>();
            wrapper.select("city_name", "ratio", "countryside_num", "rs_num");
            wrapper.eq("city_name", cityName);
            cityDetail = CommonUtils.transformClass(cityDao.selectOne(wrapper), CitySummaryVo.class);
            redisService.set(RedisConstants.CITY + "detail_" + cityName, JSON.toJSONString(cityDetail), 86400L);
        }
        return cityDetail;
    }

}